<template>
  <div class="child2">
    <h3>子组件2</h3>
	<h4>子组件2有玩具：{{ toy }}</h4>
	<!-- 直接绑定+触发事件 -->
	<button @click="emitter.emit('send-toy',toy)">给你一个玩具</button>	
  </div>
</template>

<script setup lang="ts" name="Child2">
	import {ref,onUnmounted} from 'vue'
	// import mitt from 'mitt'//文件在utils中声明暴露
	//共同的emitter实例早已在utils文件中无需创建
	import emitter from "@/utils/emitter.ts"
	const toy = ref('奥特曼')

	// 组件卸载时，移除事件，避免内存泄漏
	onUnmounted(()=>{
		emitter.off('send-toy')
	})
	
</script>

<style scoped>
	.child2{
		margin-top: 50px;
		background-color: orange;
		padding: 10px;
		box-shadow: 0 0 10px black;
		border-radius: 10px;
	}
</style>